package pl.register.android.activities.profile.logic;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import pl.register.android.R;
import pl.register.android.student.Student;

/**
 * @author Mateusz Strzelecki
 * @since 2013-04-12
 */
public class StudentEditorDialogBuilder extends AlertDialog.Builder {

    private EditText etFirstName;

    private EditText etLastName;

    private EditText etCity;

    private EditText etPhoneNo;

    private EditText etEmail;

    private EditText etClassNo;



    private StudentProfilePresenter presenter;

    private Student student;

    public StudentEditorDialogBuilder(Context context, Student student, StudentProfilePresenter presenter) {
        super(context);
        this.presenter = presenter;
        this.student = student;

        onCreateContent(context);
    }

    private void onCreateContent(Context ctx) {
        LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(ctx.LAYOUT_INFLATER_SERVICE);
        View dialogLayout = inflater.inflate(R.layout.dialog_student_editor_layout, null);
        etFirstName = (EditText) dialogLayout.findViewById(R.id.student_first_name_edit_text);
        etLastName = (EditText) dialogLayout.findViewById(R.id.student_last_name_edit_text);
        etCity = (EditText) dialogLayout.findViewById(R.id.student_city_edit_text);
        etPhoneNo = (EditText) dialogLayout.findViewById(R.id.student_phone_no_edit_text);
        etEmail = (EditText) dialogLayout.findViewById(R.id.student_email_edit_text);
        etClassNo = (EditText) dialogLayout.findViewById(R.id.student_class_no_edit_text);

        setView(dialogLayout);

        setIcon(R.drawable.ic_user_holo);
        setTitle(R.string.dialog_title_edit_student);
        setNegativeButton(R.string.btn_name_cancel, null);
        setPositiveButton(R.string.btn_name_save, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                presenter.saveStudent(getValuesFromFields(student));
            }
        });

        fillDialogFields(student);
    }

    private void fillDialogFields(Student student) {
        etFirstName.setText(student.getFirstName());
        etLastName.setText(student.getLastName());
        etCity.setText(student.getCity());
        etPhoneNo.setText(student.getPhoneNo());
        etEmail.setText(student.getEmailAddress());
        etClassNo.setText(student.getClassNo());
    }

    private Student getValuesFromFields(Student student) {
        student.setFirstName(etFirstName.getText().toString());
        student.setLastName(etLastName.getText().toString());
        student.setCity(etCity.getText().toString());
        student.setPhoneNo(etPhoneNo.getText().toString());
        student.setEmailAddress(etEmail.getText().toString());
        student.setClassNo(etClassNo.getText().toString());
        return student;
    }

}